package com.gj.fazhi.comtroller;

import com.gj.fazhi.enumpackge.StatusEnum2;
import com.gj.fazhi.mapper.FaZhiMapper;
import com.gj.fazhi.model.*;
import com.gj.fazhi.service.FaZhiService;
import com.gj.utli.ResultBuilderVo;
import com.gj.utli.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tangyunhen on 2021-06-24.
 */
@RestController
//@RequestMapping("fazhi")
@CrossOrigin //支持跨域
public class FaZhiController {

    @Autowired
    private FaZhiService fazhiService;

    //法人下拉框接口   写过了

    //备件编码接口 自动显示备件名称
    @Autowired
    private FaZhiMapper faZhiMapper;
    @GetMapping("legalPlant/query")
    public ResultVo spartCodeLike(String legalPersonCode,String spartCode){
       List<SpartVo> spartVoList= faZhiMapper.queryByCodeLike(legalPersonCode,spartCode);
      return ResultBuilderVo.success(spartVoList);
    }

    @GetMapping("querySpartByCode")
    public ResultVo querySpartByCode(String spartCode){
        List<SpartVo> spartVoList= faZhiMapper.querySpartByCode(spartCode);
        return ResultBuilderVo.success(spartVoList);
    }

    /**
     * 分页查询接口
     */
    @PostMapping("fazhi/queryPage")
    public ResultVo queryPage(@RequestBody FaZhiSelectVo faZhiSelectVo){
       return fazhiService.queryPage(faZhiSelectVo);
    }
    //添加
   @PostMapping("fazhi/add")
    public ResultVo add(@RequestBody @Valid FaZhiAddVo faZhiAddVo, BindingResult bindingResult){
      if (bindingResult.hasErrors()){
          return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
      }
         return fazhiService.add(faZhiAddVo);
    }
   //修改
    @PostMapping("fazhi/update")
    public ResultVo updape(@RequestBody @Valid FaZhiUpdateVo faZhiUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return fazhiService.update(faZhiUpdateVo);
    }




}